package com.jz.netthings.application.heatseal.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.application.heatseal.bean.HeatSeal;
import com.jz.netthings.application.heatseal.service.HeatSealService;
import com.jz.netthings.application.providerCreate.service.ProviderInfoService;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.security.utils.Token;
import com.jz.netthings.support.SimpleCoreResponse;
import com.jz.netthings.system.utils.AdscmSystemUtils;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 热合表 前端控制器
 * </p>
 *
 * @author lzf
 * @since 2019-03-18
 */
@RestController
@RequestMapping("adscm/application/heatSeal")
public class HeatSealController {
    @Autowired
    HeatSealService heatSealService;
    @Autowired
    ProviderInfoService providerInfoService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;

    @RequestMapping("queryAll")
    public SimpleCoreResponse queryAll(@RequestParam("currentPage") Integer currentPage,
                                       @RequestParam("pageSize") Integer pageSize) {
        return new SimpleCoreResponse("200", "查询成功", heatSealService.queryAll(currentPage, pageSize));
    }

    @RequestMapping("queryById")
    public SimpleCoreResponse queryById(String id) throws AdscmSystemException {
        return new SimpleCoreResponse("200", "查询成功", heatSealService.queryByid(id));
    }

    /**
     * 根据卡号查询浆员当天的热合记录
     *
     * @param cardId
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("queryByCardId")
    public SimpleCoreResponse queryByCardId(String cardId) throws AdscmSystemException {

        if (AdscmBaseUtils.getInstance().isNotEmpty(cardId, null)) {
            Wrapper<ProviderInfo> providerWrapper = new EntityWrapper<>();
            providerWrapper.eq("cardId", cardId);
            List<ProviderInfo> infos = providerInfoService.selectList(providerWrapper);
            if (infos != null && infos.size() > 0) {
                ProviderInfo info = infos.get(0);
                Wrapper<HeatSeal> wrapper = new EntityWrapper<>();
                wrapper.eq("providerNo", info.getId());
                String[] todayAndTomorrow = AdscmDateUtils.getTodayAndTomorrow(new Date());
                wrapper.between("createTime", todayAndTomorrow[0], todayAndTomorrow[1]);
                List<HeatSeal> heatSeals = heatSealService.selectList(wrapper);
                if (heatSeals != null && heatSeals.size() > 0) {
                    HeatSeal heatSeal = heatSeals.get(0);
                    return new SimpleCoreResponse("200", "查询成功", heatSeal);
                } else {
                    throw new AdscmSystemException("没有找到卡号为[" + cardId + "]的浆员当天的入库记录...");
                }
            } else {
                throw new AdscmSystemException("没有找到卡号为[" + cardId + "]的浆员，查询失败...");
            }
        } else {
            throw new AdscmSystemException("卡号为Null，查询失败...");
        }

    }

    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody HeatSeal heatSeal, HttpServletRequest request) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        return new SimpleCoreResponse("200", "修改成功", heatSealService.updateHeatSeal(heatSeal, token.getUser()));

    }

    @RequestMapping("queryByPNoAndMNo")
    public SimpleCoreResponse queryByPNoAndMNo(String pid, String mid,
                                               String sTime, Integer currentPage,
                                               Integer pageSize,
                                               String cid) throws AdscmSystemException {
        return new SimpleCoreResponse("200", "查询成功", heatSealService.queryByPNoAndMNo(
                pid, mid, sTime, currentPage, pageSize, cid
        ));
    }

    /**
     * 根据日期查询这段时间入库的所有血浆的箱号集合
     * @param startDate
     * @param endDate
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getHeatSealBoxByDate")
    public SimpleCoreResponse getHeatSealBoxByDate(String startDate,String endDate) throws AdscmSystemException {

        return new SimpleCoreResponse("200", "查询成功", heatSealService.getHeatSealBoxByDate(startDate,endDate));
    }
    /**
     * 根据箱号，查询入库记录
     * @param boxCode
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/getStockByBox")
    public SimpleCoreResponse getStockByBox(String boxCode) throws AdscmSystemException {
        return new SimpleCoreResponse("200", "查询成功", heatSealService.getStockByBox(boxCode));
    }


}

